package com.lingyun.financialmanagement.service.pojo;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingyun.financialmanagement.dao.mapper.BillMapper;
import com.lingyun.financialmanagement.dao.mapper.ComponentPartMapper;
import com.lingyun.financialmanagement.dao.mapper.ProjectMapper;
import com.lingyun.financialmanagement.dao.mapper.UsersMapper;
import com.lingyun.financialmanagement.dao.pojo.ComponentPart;
import com.lingyun.financialmanagement.dao.pojo.Project;
import com.lingyun.financialmanagement.service.ExcelService;
import com.lingyun.financialmanagement.service.ProjectService;
import com.lingyun.financialmanagement.utils.CompanyUtils;
import com.lingyun.financialmanagement.utils.ExcelSetting;
import com.lingyun.financialmanagement.utils.TimestampConverter;
import com.lingyun.financialmanagement.vo.*;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExcelServiceImpl implements ExcelService {

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private ComponentPartMapper componentPartMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectServiceImpl projectServiceImpl;
    @Autowired
    private BillMapper billMapper;
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public void excelExport() {
        // 1. 配置excel头部
        ExcelSetting.excelSetting(response);
        ExcelWriter excelWork = null;
        try{
            // 2. 将excel创建到输出流中，    同时配置时间转换类， 构建
            excelWork = EasyExcel.write(response.getOutputStream()).registerConverter(new TimestampConverter()).build();

            // 3. 使用构建出的excel进行操作
            WriteSheet sheet1 = EasyExcel.writerSheet("工作表1").head(ComponentPart.class).registerConverter(new TimestampConverter()).build();
            List<ComponentPart> componentParts = componentPartMapper.selectList(null);
            excelWork.write(componentParts, sheet1);

            // 4. 重复操作即可生成多个表
            WriteSheet sheet2 = EasyExcel.writerSheet("工作表2").head(ComponentPart.class).registerConverter(new TimestampConverter()).build();
            List<ComponentPart> componentParts1 = componentPartMapper.selectList(null);
            excelWork.write(componentParts1, sheet2);

        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            if (excelWork != null){
                excelWork.finish();
            }
        }
//        List<ComponentPart> componentParts = componentPartMapper.selectList(null);
//        try {
//            EasyExcel.write(response.getOutputStream(), ComponentPart.class).registerConverter(new TimestampConverter()).sheet("用户信息2").doWrite(componentParts);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
    }

    @Override
    public void excelGeneralLedger() {
        ExcelSetting.excelSetting(response);
        ExcelWriter excelWork = null;
        try{
            // 2. 将excel创建到输出流中，    同时配置时间转换类， 构建
            excelWork = EasyExcel.write(response.getOutputStream()).registerConverter(new TimestampConverter()).build();

            // 3. 使用构建出的excel进行操作
            WriteSheet sheet1 = EasyExcel.writerSheet("总账").head(GeneralLedger.class).registerConverter(new TimestampConverter()).build();

            ArrayList<GeneralLedger> generalLedgers = CompanyUtils.getCompanyCost(projectMapper);

            excelWork.write(generalLedgers, sheet1);

            // 设置要查找的项目转为excel
            QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
            projectQueryWrapper.orderByAsc("create_time");
            List<Project> projects = projectMapper.selectList(projectQueryWrapper);
            for (int i = 0; i < projects.size(); i++) {
                WriteSheet sheet = EasyExcel.writerSheet(projects.get(i).getName()).head(ComponentPartExcel.class).registerConverter(new TimestampConverter()).build();
                List<ComponentPartExcel> componentPartExcel = CompanyUtils.getComponentPartExcel(projects, i, componentPartMapper, billMapper, usersMapper);
                excelWork.write(componentPartExcel, sheet);
            }

        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            if (excelWork != null){
                excelWork.finish();
            }
        }
    }
}
